import sys
sys.path.append("../mysql/")
sys.path.append("../mongo/")
sys.path.append("../")
import mongo_helper2
import mylogger
import jieba
import math
import time

def process_query(query):
    res = query.split(" ")
    query_items = []
    with open("D:\\workstation\\information_retrieval\\ir\\stop_word", "r", encoding="utf-8") as file2:
        stop_word = file2.readlines()
        stop_word = [word.replace("\n", "") for word in stop_word]
    for words in res:
        if len(words) == 0:
            continue
        seg_list = jieba.cut(words)
        query_item = []
        for seg in seg_list:
            if seg in stop_word or len(seg) == 0:
                continue
            query_item.append(seg)
        query_items.append(query_item)
    return query_items

def get_intersection(index, doc_num):
    #求倒排索引的交集
    # 我觉得做并集
    '''
    一个词项对应着t1: [ [25, 34], [25, 67], [45, 34] ]
    一个词项对应着t2: [ [25, 34], [25, 67], [45, 34] ]
    '''

    docid_list = []
    for i in range(len(index[0])):
        mark = True
        total_tf_idf = (1 + math.log(index[0][i][1])) * math.log(doc_num / len(index[0]))
        for j in range(1, len(index)):
            #每个查询词项的tf-idf
            flag = False
            for k in range(len(index[j])):
                if index[0][i][0] == index[j][k][0]:
                    flag = True
                    total_tf_idf += (1 + math.log(index[j][k][1])) * math.log(doc_num / len(index[j]))
                    break
            if flag == False:
                #说明当前的docid = index[0][i][0]交集为空
                mark = False
                break

        if mark:
            docid_list.append([index[0][i][0], total_tf_idf])
    return docid_list

def merge_docid(search_res, docid_list):
    for i in range(len(docid_list)):
        mark = False
        for j in range(len(search_res)):
            if docid_list[i][0] == search_res[j][0]:
                search_res[j][1] += docid_list[i][1]
                mark = True
                break
        if not mark:
            search_res.append(docid_list[i])

    #按照tf-idf的值排序
    for i in range(len(search_res) - 1):
        for j in range(len(search_res) - 1, i, -1):
            if search_res[j][1] > search_res[j-1][1]:
                tmp = search_res[j]
                search_res[j] = search_res[j - 1]
                search_res[j - 1] = tmp
    return search_res



def search_item(query):
    begin_time = time.time()
    doc_num = 1153700
    # 2021年12月14
    query_items = process_query(query)

    search_res = []
    for query_item in query_items:
        query_item_index = []
        for item in query_item:
            # 1217
            item_res = mongo_helper2.find_by_item(item)
            if item_res is not None:
                item_index = item_res.get("index")
                query_item_index.append(item_index)
            else:
                break
            # 1217

        end_time = time.time()
        print("mongodb查询时间 {} 秒".format(end_time - begin_time))

        # 1217
        if len(query_item_index) != 0:
            docid_list = get_intersection(query_item_index, doc_num)
        else:
            docid_list = []
        # 1217

        search_res = merge_docid(search_res, docid_list)
    res_docid_list = []
    for res in search_res:
        res_docid_list.append(res[0])
    return res_docid_list


if __name__ == "__main__":

    pass

    #print(process_query("生活中那些让人破防的拼搏瞬间"))
    #print(process_query("【致敬！#生活中那些让人破防的拼搏瞬间#】在我们看得见、看不见的角落里，有无数努力拼搏的人，无论他们身在乡村还是城市，无论从事何种职业，都在拼尽全力地奋斗着。正因如此，才让这个世界变得更加美好。致敬每一个奋斗者！  http://t.cn/A6Ms2BN0 ​​​"))
    #mylogger.logger.error("qqq")
